//
//  SLNearlyCollectionViewCell.m
//  StarLight
//
//  Created by Duke.wu on 16/5/16.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLNearlyCollectionViewCell.h"

@interface SLNearlyCollectionViewCell()




@end


@implementation SLNearlyCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame{
  self = [super initWithFrame:frame];
  if (self) {
    self.layer.cornerRadius = 5.f;
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.lfTopLB];
    [self addSubview:self.lfSecLB];
    [self addSubview:self.lfThidLB];
    [self addSubview:self.lfForthLB];
    [self addSubview:self.lfFifLB];
    [self addSubview:self.rgSecLB];
    [self addSubview:self.rgThidLB];
    [self addSubview:self.rgForthLB];
    [self addSubview:self.rgFifLB];
    [self addSubview:self.sinceTimeLB];
    [self addSubview:self.midImageView];
    [self addSubview:self.rightBtn];
    
    [self addUIConstraints];
  }
  return self;
}


- (void)tapCurrentLocBtn{
  if (_delegate && [_delegate respondsToSelector:@selector(currentBtnWasTapedWithShipModel:)]) {
    [_delegate currentBtnWasTapedWithShipModel:self.shipModel];
  }
}


#pragma mark - Public

- (void)configNearlyCellWithProductModel:(SLNearLyProduct *)productModel{
  self.goodsModel = productModel;
  self.lfTopLB.text = productModel.hwmc;
  self.lfThidLB.text = productModel.qsgk;
  self.lfFifLB.text = productModel.jzrq;
  self.sinceTimeLB.text = productModel.time;
  self.rgThidLB.text = productModel.ddgk;
  self.rgFifLB.text = [NSString stringWithFormat:@"%.2f 公里",productModel.distance.floatValue];
  if (productModel.hwzw.integerValue) {
    self.currrntWeiLB.text = [NSString stringWithFormat:@"%@ 吨",productModel.hwzw];
  }else{
    self.currrntWeiLB.text = [NSString stringWithFormat:@"%@",productModel.hwzw];
  }
  
}

- (void)configNearlyCellWithShipModel:(SLNearLyShip *)shipModel{
  self.shipModel = shipModel;
  self.lfTopLB.text = shipModel.boatname;
  self.lfFifLB.text = shipModel.shiploc;
  self.sinceTimeLB.text = shipModel.statetime;
  self.rgFifLB.text = [NSString stringWithFormat:@"%.2f 公里",shipModel.distance];

  switch (shipModel.state) {
    case 0:{
     //满载
      self.lfThidLB.text = @"满载";
      self.rgThidLB.text = @"0";
      self.shipStatusImageView.image = [UIImage imageNamed:@"Full load"];
    }
      break;
    case 1:{
      //空船找货
      self.lfThidLB.text = @"空船找货";
      self.rgThidLB.text = [NSString stringWithFormat:@"%ld 吨",(long)shipModel.loads];
      self.shipStatusImageView.image = [UIImage imageNamed:@"Find goods"];
    }
      break;
    case 2:{
      //半船找货
      self.lfThidLB.text = @"半船找货";
      self.rgThidLB.text = [NSString stringWithFormat:@"%@ 吨",shipModel.freespace];

      self.shipStatusImageView.image = [UIImage imageNamed:@"半船找货"];
    }
      break;
    case 3:{
      //预定货物
      self.lfThidLB.text = @"预定货物";
      self.rgThidLB.text = [NSString stringWithFormat:@"%ld 吨",(long)shipModel.loads];
      self.shipStatusImageView.image = [UIImage imageNamed:@"预定货物"];
      self.lfFifLB.text = shipModel.goalloc;
    }
      break;
      
    default:
      break;
  }
}

#pragma mark - private

- (void)configWithType:(NearlyCellType)type{
  self.cellType = type;
  if (type == NearlyCellTypeShip ) {
    self.lfSecLB.text = @"起点";
    self.lfForthLB.text = @"截止日期";
    self.rgSecLB.text = @"终点";
    [self addSubview:self.weightLB];
    [self addSubview:self.currrntWeiLB];
    self.midImageView.hidden = NO;
    
    [self.weightLB mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.lfForthLB);
      make.centerX.equalTo(self);
    }];
    
    [self.currrntWeiLB mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.lfFifLB);
      make.centerX.equalTo(self);
    }];
    
    
  }else{
    self.lfSecLB.text = @"当前状态";
    self.lfForthLB.text = @"当前位置";
    self.rgSecLB.text = @"装载量";
    self.midImageView.hidden = YES;
    [self addSubview:self.shipStatusImageView];
    [self addSubview:self.currentLocBtn];
    
    [self.shipStatusImageView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.lfThidLB);
      make.left.equalTo(self.lfThidLB.mas_right).offset(10);
      make.size.mas_equalTo(CGSizeMake(23, 23));
    }];
    
    [self.currentLocBtn mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.lfFifLB);
      make.left.equalTo(self.lfFifLB.mas_right).offset(10);
      make.size.mas_equalTo(CGSizeMake(40, 23));
    }];
  }
}

- (void)addUIConstraints{
  
  [self.sinceTimeLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self).offset(8);
    make.centerX.equalTo(self);
  }];
  
  UIImageView *timeimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"time2"]];
  [self addSubview:timeimageView];
  [timeimageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.sinceTimeLB);
    make.right.equalTo(self.sinceTimeLB.mas_left).offset(-2);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];

  
  [self.lfTopLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self).offset(18);
    make.top.equalTo(self).offset(8);
    make.right.equalTo(timeimageView.mas_left).offset(-2);
  }];
  
  [self.lfSecLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.lfTopLB);
    make.top.equalTo(self.lfTopLB.mas_bottom).offset(12);
  }];
  
  [self.lfThidLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.lfTopLB);
    make.top.equalTo(self.lfSecLB.mas_bottom).offset(12);
    make.width.lessThanOrEqualTo(@(120 * MIX_W));
  }];
  
  [self.midImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self);
    make.centerY.equalTo(self.lfThidLB);
    make.size.mas_equalTo(CGSizeMake(22, 22));
  }];
  
  [self.lfForthLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.lfTopLB);
    make.top.equalTo(self.lfThidLB.mas_bottom).offset(12);
  }];
  
  [self.lfFifLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.lfTopLB);
    make.width.lessThanOrEqualTo(@(120 * MIX_W));
    make.top.equalTo(self.lfForthLB.mas_bottom).offset(12);
  }];
  
  [self.rgSecLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self).offset(-18);
    make.centerY.equalTo(self.lfSecLB);
  }];
  
  [self.rgThidLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.rgSecLB);
    make.width.equalTo(@70);
    make.centerY.equalTo(self.lfThidLB);
  }];
  
  [self.rgForthLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.rgSecLB);
    make.centerY.equalTo(self.lfForthLB);
  }];
  
  [self.rgFifLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.rgSecLB);
    make.centerY.equalTo(self.lfFifLB);
  }];
  
  

  
  UIView *line = [UIView lineView];
  [self addSubview:line];
  [line mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@1);
    make.left.width.equalTo(self);
    make.top.equalTo(self).offset(30);
  }];
  
  UIView *shortLine = [UIView lineView];
  [self addSubview:shortLine];
  [shortLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.lfSecLB);
    make.right.equalTo(self.rgSecLB);
    make.top.equalTo(self.lfThidLB.mas_bottom).offset(5);
    make.height.equalTo(@0.5);
  }];
  
  
  [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.lfTopLB);
    make.right.equalTo(self.rgSecLB);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  
  
}
#pragma mark - getter

- (UIButton *)rightBtn{
  if (!_rightBtn) {
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_rightBtn setImage:[UIImage imageNamed:@"next"] forState:UIControlStateNormal];
  }
  return _rightBtn;
}


- (UIImageView *)midImageView{
  if (!_midImageView) {
    _midImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"boat2"]];
  }
  return _midImageView;
}

- (UILabel *)lfTopLB{
  if (!_lfFifLB) {
    _lfTopLB = [UILabel labelwithBoldFontSize:14 textColor:[UIColor hexStringToColor:@"#fe9f00"] text:@""];
  }
  return _lfTopLB;
}

- (UILabel *)lfSecLB{
  if (!_lfSecLB) {
    _lfSecLB = [UILabel labelWithFontSize:12 textColor:SLGrayColor];
  }
  return _lfSecLB;
}

- (UILabel *)lfThidLB{
  if (!_lfThidLB) {
    _lfThidLB = [UILabel labelwithBoldFontSize:14 textColor:SLBlackColor text:@""];
  }
  return _lfThidLB;
}

- (UILabel *)lfForthLB{
  if (!_lfForthLB) {
    _lfForthLB = [UILabel labelWithFontSize:12 textColor:SLGrayColor];
  }
  return _lfForthLB;
}

- (UILabel *)lfFifLB{
  if (!_lfFifLB) {
    _lfFifLB = [UILabel labelwithBoldFontSize:12 textColor:SLBlackColor text:@""];
  }
  return _lfFifLB;
}

- (UILabel *)rgSecLB{
  if (!_rgSecLB) {
    _rgSecLB = [UILabel labelWithFontSize:12 textColor:SLGrayColor];
  }
  return _rgSecLB;
}

- (UILabel *)rgThidLB{
  if (!_rgThidLB) {
    _rgThidLB = [UILabel labelwithBoldFontSize:14 textColor:SLBlackColor text:@""];
    _rgThidLB.textAlignment = NSTextAlignmentRight;
  }
  return _rgThidLB;
}

- (UILabel *)rgForthLB{
  if (!_rgForthLB) {
    _rgForthLB = [UILabel labelWithFontSize:12 textColor:SLGrayColor text:@"距离你"];
  }
  return _rgForthLB;
}

- (UILabel *)rgFifLB{
  if (!_rgFifLB) {
    _rgFifLB = [UILabel labelwithBoldFontSize:12 textColor:SLBlackColor text:@""];
  }
  return _rgFifLB;
}

- (UILabel *)sinceTimeLB{
  if (!_sinceTimeLB) {
    _sinceTimeLB = [UILabel labelWithFontSize:12 textColor:SLGrayColor text:@"lallalalal"];
  }
  return _sinceTimeLB;
}

- (UILabel *)weightLB{
  if (!_weightLB) {
    _weightLB = [UILabel labelWithFontSize:12 textColor:SLGrayColor text:@"重量"];
  }
  return _weightLB;
}

- (UILabel *)currrntWeiLB{
  if (!_currrntWeiLB) {
    _currrntWeiLB = [UILabel labelWithFontSize:12 textColor:[UIColor blackColor]];
  }
  return _currrntWeiLB;
}

- (UIButton *)currentLocBtn{
  if (!_currentLocBtn) {
    _currentLocBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_currentLocBtn setImage:[UIImage imageNamed:@"map2"] forState:UIControlStateNormal];
    [_currentLocBtn addTarget:self action:@selector(tapCurrentLocBtn) forControlEvents:UIControlEventTouchUpInside];
  }
  return _currentLocBtn;
}

- (UIImageView *)shipStatusImageView{
  if (!_shipStatusImageView) {
    _shipStatusImageView = [[UIImageView alloc] init];
  }
  return _shipStatusImageView;
}

@end
